package com.jason.concurrent.pool;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author  yslai
 * @Date  2022/9/20 10:15
 * @Description 创建三种线程池
 */
public class ExcutorDemo {

    //第一种创建一池n线程
    // 第二种创建一池一线程
    //可扩容的线程池

    public static void main(String[] args) {
        //1->n
//        ExecutorService executorPool1 = Executors.newFixedThreadPool(5);
       //1->1
//        ExecutorService executorPool2 = Executors.newSingleThreadExecutor();
        ExecutorService executorPool3 = Executors.newCachedThreadPool();


//        try {
//            for (int i = 0; i < 10; i++) {
//                executorPool1.execute(()->{
//                    System.out.println(Thread.currentThread().getName()+"正在办理业务");
//                });
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }finally {
//            executorPool1.shutdown();
//        }

//        try {
//            for (int i = 0; i < 10; i++) {
//                executorPool2.execute(()->{
//                    System.out.println(Thread.currentThread().getName()+"正在办理业务");
//                });
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }finally {
//            executorPool2.shutdown();
//        }
        try {
            for (int i = 0; i < 10; i++) {
                executorPool3.execute(()->{
                    System.out.println(Thread.currentThread().getName()+"正在办理业务");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            executorPool3.shutdown();
        }
    }
}
